
import java.util.ArrayList;
import java.util.Scanner;

import dao.ClassDao;
import dao.CourseDao;
import dao.ScoreDao;
import dao.StudentDao;
import model.Course;
import model.Score;
import model.Student;
import util.PrintUtil;

// 导入包的快捷键 CTRL+SHIFT+O

public class Main {
	private static ArrayList<Student> studentList = new ArrayList<>();
	static boolean isMenuShow = false;

	public static void main(String[] args) {
		showMenu();
	}

	private static void showMenu() {
		while (true) {
			// 为了避免程序疯狂运行造成死机 
			// 每秒钟检查一次是否要展示菜单
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			if (!isMenuShow) {
				PrintUtil.printMenu();
				isMenuShow = true;
				
				Scanner sc = new Scanner(System.in);
				int i;
				try {
					i = sc.nextInt();
					switch (i) {
					case 1:
						studentList = StudentDao.queryStudentList();
						PrintUtil.print(studentList);
						break;
					case 2:
						// 获取学生信息
						Student student = getStudentInfo();
						// 添加学生
//						StudentDao.addUseStatement(student);
						StudentDao.addUsePrepareStatement(student);
						break;
					case 3:
						// 获取用户输入的学生姓名
						String name = getStudentName();
						// 按照姓名删除学生 
						StudentDao.del(name);
						
						break;
					case 4:
						// 获取要修改的学生信息
						String stuname = getStudentName();
						// 获取要修改的字段
						int column = getUpdateColumn();
						// 获取要修改的值
						String value = getNewValue();
						// 更新学生表
						StudentDao.update(stuname, column, value);
						break;
					case 5:
						// 获取要修改的学生信息
						String stu_name = getStudentName();
						// 通过学生姓名获取到学生的ID
						int stu_id = StudentDao.getStuIdWithName(stu_name);
						// 通过CourseDao获取课程列表
						ArrayList<Course> courseList =  CourseDao.queryCourseList();
						// 打印课程列表
						PrintUtil.printCourse(courseList);
						// 获取用户选择的课程ID
						int course_id = getSeletedCouseID();
						// 获取学生的选课成绩
						int score = getCourseScore();
						// 插入选课和成绩
						ScoreDao.add(stu_id, course_id, score);
					case 6:
						// 查看成绩
						
						// 获取要查询的学生姓名
						String nameStu = getStudentName();
						int studentid = StudentDao.getStuIdWithName(nameStu);
						
						// 通过学号查找该学生的成绩列表
						ArrayList<Score> scoreList = ScoreDao.getScoreList(studentid);
						// 打印成绩列表
						PrintUtil.printScore(scoreList);
						
					case 7:
						// 退出程序 ---关闭数据库连接
						System.exit(0);
						break;
					default:
						System.out.println("您的输入有误，请重新输入");
						break;
					}
					
				} catch (Exception e) {
					System.out.println("您的输入有误，请重新输入");
				}			
				
				isMenuShow = false;
			}
		}	
	}

	private static int getCourseScore() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入课程成绩");
		int value = in.nextInt();
		return value;
	}

	private static int getSeletedCouseID() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入课程ID");
		int value = in.nextInt();
		return value;
	}

	private static String getNewValue() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入新的值");
		String value = in.nextLine();
		return value;
	}

	private static int getUpdateColumn() {
		Scanner in = new Scanner(System.in);
		System.out.println("请选择要更新的字段");
		System.out.println("1.姓名");
		System.out.println("2.年龄");
		System.out.println("3.性别");
		
		int result = in.nextInt();
		return result;
	}

	private static String getStudentName() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入姓名");
		String name = in.nextLine();
		return name;
	}

	private static Student getStudentInfo() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入学号");
		int stuid = in.nextInt();
		
		in = new Scanner(System.in);
		System.out.println("请输入姓名");
		String name = in.nextLine();
		
		in = new Scanner(System.in);
		System.out.println("请输入年龄");
		int age = in.nextInt();
		
		in = new Scanner(System.in);
		System.out.println("请输入班级名称");
		String class_name = in.nextLine();
		
		in = new Scanner(System.in);
		System.out.println("请输入专业");
		String class_major = in.nextLine();
		
		
		in = new Scanner(System.in);
		System.out.println("请输入性别");
		String gender = in.nextLine();
		
		System.out.println("学生信息输入完成");
		
		// 智能提示自动完成   ALT+？
		Student student =  new Student(stuid, name, age, class_name, class_major, gender);
		return student;
	}
}
